package jrobutt;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;

public class Common
{
    public final static int BITS_PER_BYTE = 8;
    
    public final static int DEFAULT_BUFFER_LENGTH = 1024 * 10;
    
    private final static Font error_font;
    private final static JPanel error_panel;
    private final static JLabel error_header;
    private final static JTextArea error_textarea;
    private final static JLabel error_footer;
    
    static
    {
        try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
        catch (Exception ex) {}
        
        error_font = new Font("verdana", Font.PLAIN, 12);
        
        error_panel = new JPanel(new BorderLayout());
        
        error_header = new JLabel();
        error_header.setFont(error_font);
        
        error_textarea = new JTextArea();
        error_textarea.setEditable(false);
        error_textarea.setFont(error_font);
        error_textarea.setBorder(new BevelBorder(BevelBorder.LOWERED));
        
        error_footer = new JLabel();
        error_footer.setFont(error_font);
        
        error_panel.add(error_header, BorderLayout.NORTH);
        {
            JScrollPane spane = new JScrollPane(error_textarea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            spane.setBorder(null);
            spane.setPreferredSize(new Dimension(640, 240));
            error_panel.add(spane, BorderLayout.CENTER);
        }
        error_panel.add(error_footer, BorderLayout.SOUTH);
    }
    
    public static void show_error (Throwable ex, String header, String footer, String... additional_info)
    {
        StringBuilder err_msg = new StringBuilder(ex.toString() + "\n");
        for (StackTraceElement element : ex.getStackTrace())
        {
            err_msg.append("\t" + element + "\n");
        }
        for (String line : additional_info)
        {
            err_msg.append(line + "\n");
        }
        err_msg.deleteCharAt(err_msg.length()-1);
        error_textarea.setText(err_msg.toString());
        
        error_header.setText(header);
        error_footer.setText(footer);
        JOptionPane.showMessageDialog(null, error_panel, "Error", JOptionPane.ERROR_MESSAGE);
    }
    
    
    
    public static void connect (InputStream istream, OutputStream ostream) throws IOException
    {
        byte[] buffer = new byte[DEFAULT_BUFFER_LENGTH];
        int n;
        while ((n = istream.read(buffer, 0, buffer.length)) > 0)
        {
            ostream.write(buffer, 0, n);
        }
    }
    
    public static byte[] read_all_as_bytes (InputStream istream) throws IOException
    {
        ByteArrayOutputStream byte_stream = new ByteArrayOutputStream();
        connect(istream, byte_stream);
        return byte_stream.toByteArray();
    }
    
    public static String read_all_as_string (InputStream istream) throws IOException
    {
        return new Scanner(istream).useDelimiter("\\A").next();
    }
}
